package com.autonavi.yunda.yunji.core.enums;

import com.autonavi.yunda.yunji.common.utils.SpringUtil;
import com.autonavi.yunda.yunji.core.service.dbfactory.*;

/**
 * @author cizhong.wcs
 * @date 2022/7/6 19:52
 */
public enum DatasourceType {
    MYSQL(DbFactoryService.class),
    TDDL(DbFactoryService.class),
    PGSQL(DbFactoryService.class),
    REDIS(RedisFactoryService.class),
    OSS(OssFactoryService.class),
    LINDORM(LindormFactoryService.class),
    TAIR(TairFactoryService.class),
    ;
    private final Class<? extends AbstractDataSourceFactory<?>> factoryClass;

    DatasourceType(Class<? extends AbstractDataSourceFactory<?>> factoryClass) {
        this.factoryClass = factoryClass;
    }

    /**
     * 判断数据源是否可本地化
     *
     * @param type 数据源类型
     * @return true 可本地化 false 不可本地化
     */
    public static boolean canExportType(DatasourceType type) {
        switch (type) {
            case MYSQL:
            case REDIS:
            case PGSQL:
                return true;
            case TDDL:
            case OSS:
            case LINDORM:
            case TAIR:
            default:
                return false;
        }
    }

    public AbstractDataSourceFactory<?> getFactoryBean() {
        return SpringUtil.getBean(factoryClass);
    }
}
